<?php

// use Illuminate\Support\Facades\Schema;
use Jialeo\LaravelSchemaExtend\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitJika extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('jika_activity', function (Blueprint $table) {
            $table->comment = '集卡';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->string('title', 255)->nullable()->comment('标题'); // 标题
            $table->string('sub_title', 255)->nullable()->comment('副标题'); // 副标题

            $table->string('thumb', 255)->nullable()->comment('封面'); // 封面
            $table->json('images')->nullable()->comment('轮播图');
            $table->json('detail_images')->nullable()->comment('详情图');

            $table->boolean('is_payable')->default(0)->comment('是否可通过支付来兑换抽卡次数'); 

            $table->boolean('is_inviteable')->default(0)->comment('是否可通过分享来获得次数'); 
            $table->tinyInteger('invite_reward_times')->default(1)->comment('每邀请1人奖励的次数'); 

            $table->integer('money_price')->index()->nullable()->comment('人民币价格'); // 支付价格
            $table->integer('score_price')->index()->nullable()->comment('积分价格'); // 支付价格

            $table->integer('visit_total')->default(0)->comment('访问人数'); // 访问人数

            $table->integer('stock')->nullable()->comment('剩余库存'); // 剩余库存
            $table->integer('sales')->default(0)->comment('销量'); 

            $table->json('join_codes')->nullable()->comment('参与码'); // 参与码

            $table->timestamp('start_at')->nullable()->index()->comment('开始时间');
            $table->timestamp('end_at')->nullable()->index()->comment('结束时间');

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');


            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('jika_cards', function (Blueprint $table) {
            $table->comment = '集卡中的卡片';

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('activity_id')->index()->nullable()->comment('活动id');

            $table->string('title', 100)->nullable()->comment('标题'); // 标题
            $table->string('number', 255)->nullable()->comment('编号'); // 副标题
            $table->string('thumb', 255)->nullable()->comment('封面'); // 副标题

            $table->integer('stock')->default(0)->comment('奖品库存'); // 销量
            $table->integer('sales')->default(0)->comment('奖品销量'); // 销量

            $table->float('odds')->nullable()->comment('获得概率');

            $table->integer('min_open_index')->nullable()->index()->comment('最少开出序号');
            $table->integer('last_open_index')->default(0)->index()->comment('上次开出序号');
            $table->integer('next_open_index')->default(0)->index()->comment('下次开出序号');
            $table->integer('interval')->default(0)->comment('开出间隔');

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');

            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('jika_remain_times', function (Blueprint $table) {
            $table->comment = '用户剩余次数';

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('activity_id')->index()->nullable()->comment('活动id');

            $table->integer('user_id')->index()->nullable()->comment('用户id');

            $table->integer('stock')->index()->default(0)->comment('剩余次数');
            $table->integer('sales')->index()->default(0)->comment('已使用次数');
            $table->integer('pay_total')->index()->default(0)->comment('使用支付换来的次数');

            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('jika_user_cards', function (Blueprint $table) {
            $table->comment = '用户拥有的卡片';

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('activity_id')->index()->nullable()->comment('活动id');
            $table->integer('card_id')->index()->nullable()->comment('卡片id');
            $table->integer('user_id')->index()->nullable()->comment('用户id');

            $table->integer('stock')->index()->default(0)->comment('拥有库存');
            $table->integer('sales')->index()->default(0)->comment('已使用');
            
            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('jika_card_records', function (Blueprint $table) {
            $table->comment = '用户的卡片记录';

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('activity_id')->index()->comment('活动id');

            $table->integer('card_id')->index()->comment('卡片id');
            $table->integer('user_id')->index()->comment('用户id');
            $table->integer('total')->default(0)->comment('变动数量');

            $table->tinyInteger('type')->default(1)->index()->comment('变动类型, 1为抽卡收入 2为兑换支出');
            
            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('jika_skus', function (Blueprint $table) {
            $table->comment = '集卡可兑换的奖品';

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('activity_id')->index()->nullable()->comment('活动id');

            $table->tinyInteger('sku_type')->default(1)->comment('奖品类型, 1为自定义实物，2为绑定商城商品 3为平台虚拟资产  4为无任何奖品');

            $table->json('assets')->nullable()->comment('当sku_type为2时，绑定的虚拟资产'); 
            $table->integer('product_id')->index()->nullable()->comment('当sku_type为1时, 绑定的商品id');

            $table->string('title', 100)->nullable()->comment('标题'); // 标题
            $table->string('number', 255)->nullable()->comment('编号'); // 副标题
            $table->string('thumb', 255)->nullable()->comment('封面'); // 副标题


            $table->integer('money_price')->unsigned()->nullable()->comment('人民币价格');     // 价格
            $table->integer('score_price')->unsigned()->nullable()->comment('积分价格');     // 价格

            $table->integer('stock')->default(0)->comment('奖品库存'); // 销量
            $table->integer('sales')->default(0)->comment('奖品销量'); // 销量

            $table->json('need_cards')->nullable()->comment('兑换此奖品需要的卡片数');

            // $table->float('odds')->nullable()->comment('中奖几率, 30表示30%');

            $table->integer('level')->default(1)->index()->comment('sku等级，一共5个等级');

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');

            $table->softDeletes();
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('jika_activity');
        Schema::dropIfExists('jika_cards');
        Schema::dropIfExists('jika_remain_times');
        Schema::dropIfExists('jika_user_cards');
        Schema::dropIfExists('jika_skus');
    }
}
